package com.punai.config.controller;

import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.config.constants.PnConfigConstants;
import com.punai.config.service.ConfigFeatureElementService;
import com.punai.config.service.ConfigGenerateCodeService;
import com.punai.config.service.ConfigProjectFeatureService;
import com.punai.config.sqlitedomain.ConfigFeatureElement;
import com.punai.config.sqlitedomain.vo.ConfigFeatureElementVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/config/projectFeature")
public class ConfigProjectFeatureController extends BaseController {

    @Autowired
    private ConfigGenerateCodeService configGenerateCodeService;
    @Autowired
    private ConfigProjectFeatureService configProjectFeatureService;

    @PreAuthorize("@ss.hasPermi('config:projectFeature:list')")
    @GetMapping("/list")
    public AjaxResult list(ConfigFeatureElementVo vo) throws ParseException {
        Page<ConfigFeatureElement> resultList = configProjectFeatureService.selectList(vo);
        List<ConfigFeatureElement> content = resultList.getContent();
        return AjaxResult.success(content);
    }

    @PreAuthorize("@ss.hasPermi('config:projectFeature:addedit')")
    @PostMapping
    public AjaxResult add(@RequestBody ConfigFeatureElement vo) throws ParseException {
        return AjaxResult.success(configProjectFeatureService.add(vo));
    }

    @PreAuthorize("@ss.hasPermi('config:projectFeature:query')")
    @GetMapping("/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) throws ParseException {
        ConfigFeatureElement configFeatureElement = configProjectFeatureService.getInfo(id);
        return AjaxResult.success(configFeatureElement);
    }

    @PreAuthorize("@ss.hasPermi('config:projectFeature:remove')")
    @DeleteMapping("{id}")
    public AjaxResult delInfo(@PathVariable("id") String id) {
        return AjaxResult.success(configProjectFeatureService.deleteInfo(id));
    }

    @PreAuthorize("@ss.hasPermi('config:projectFeature:query')")
    @GetMapping("checkCalculateCaliberType")
    public AjaxResult checkCalculateCaliberType(ConfigFeatureElement configFeatureElement) throws ParseException {
        String formatId = configFeatureElement.getFormatId();
        boolean isExist = configProjectFeatureService.configProjectFeatureService(formatId);
        return AjaxResult.success(isExist);
    }

    @PreAuthorize("@ss.hasPermi('config:projectFeature:copy')")
    @PostMapping("/copyOtherClassNode/{formatIds}/{elementIds}")
    public AjaxResult copyOtherClassNode(@PathVariable("formatIds") String formatId,@PathVariable("elementIds") String elementIds) throws ParseException {
        return AjaxResult.success(configProjectFeatureService.copyOtherClassNode(formatId, elementIds));
    }

    @PreAuthorize("@ss.hasPermi('config:featureElement:query')")
    @GetMapping("getNextCode")
    public AjaxResult getNextCode() {
        String nextCode = configGenerateCodeService.getNextCode(PnConfigConstants.CODE_TYPE.ELE_CODE);
        return AjaxResult.success("成功",nextCode);
    }

    @PreAuthorize("@ss.hasPermi('config:featureElement:query')")
    @PostMapping("/moveup/{elementIds}/{prevId}")
    public AjaxResult moveup(@PathVariable("elementIds") String elementIds,@PathVariable(value = "prevId",required = false) String prevId) throws ParseException {
        boolean s=configGenerateCodeService.moveToFormatForm(elementIds,null,prevId);
        if (s){
            return AjaxResult.success("操作成功!");
        }else {
            return AjaxResult.error("操作失败!");
        }
    }

    @PreAuthorize("@ss.hasPermi('config:featureElement:query')")
    @PostMapping("/movedown/{elementIds}/{nextId}")
    public AjaxResult movedown(@PathVariable("elementIds") String elementIds,@PathVariable(value = "nextId",required = false) String nextId) throws ParseException {
        boolean s=configGenerateCodeService.moveToFormatForm(elementIds,nextId,null);
        if (s){
            return AjaxResult.success("操作成功!");
        }else {
            return AjaxResult.error("操作失败!");
        }
    }



}
